//
//  MapTopView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/23.
//

import UIKit

class MapTopView: UIView {
    
    lazy var bgImage: UIView = {
        let gradientView = UIView(frame: .init(x: 0, y: 0, width: ScreenW, height: 64))
        let gradient = CAGradientLayer()
        gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
        gradient.colors = [UIColor.black.withAlphaComponent(0.5).cgColor, UIColor.black.withAlphaComponent(0).cgColor]
        gradient.locations = [0, 1]
        gradient.frame = gradientView.bounds
        gradientView.layer.addSublayer(gradient)
        return gradientView
    }()
    
    lazy var cityBgView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(white: 0, alpha: 0.2)
        view.wn_cornerWithBorder(16)
        return view
    }()
    
    lazy var cityLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textAlignment = .center
        label.textColor = .white
        return label
    }()
    
    lazy var userBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "placeholder_header"), for: .normal)
        btn.imageView?.contentMode = .scaleAspectFill
        btn.rx.tap.bind { [weak self] in
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(14, 1, .init(named: "red_FE95B4")!)
        return btn
    }()
    
    lazy var positionBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage(UIImage(named: "social_position"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.topClosure?("position")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
        
    var topClosure: StringClosure?
            
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bgImage)
        bgImage.addSubview(cityBgView)
        cityBgView.addSubview(cityLabel)
        cityBgView.addSubview(userBtn)
        addSubview(positionBtn)
        cityBgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(12)
            make.width.equalTo(scaleWidth(160))
            make.height.equalTo(32)
        }
        cityLabel.snp.makeConstraints { (make) in
            make.center.equalTo(cityBgView.snp.center)
        }
        userBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(cityBgView.snp.centerY)
            make.left.equalTo(cityBgView.snp.left).offset(2)
            make.width.height.equalTo(28)
        }
        positionBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(cityBgView.snp.centerY)
            make.right.equalTo(-12)
            make.height.width.equalTo(40)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
